import { defineStore } from 'pinia'
import { ref } from 'vue'
export const useUserStore = defineStore(
  'user',
  () => {
    const userInfo = ref({})
    const setUserInfo = (obj) => {
      userInfo.value = obj
    }
    const setUserAvatar = (url) => {
      userInfo.value.avatar = url
    }
    // 更新用户身份
    const updateMember = (num) => {
      userInfo.value.member = num
    }

    // 更新用户积分
    const updatePoint = (point) => {
      userInfo.value.points = point
    }
    
    // 更新用户余额
    const updateBalance = (balance) => {
      userInfo.value.balance = balance
    }

    // 收藏信息
    const starList = ref([])
    const setStarList = (list) => {
      starList.value = list
    }
    // 插入一条收藏数据
    const insertStarList = (obj) => {
      starList.value.push(obj)
    }
    // 移除收藏
    const delStarList = (dessertId) => {
      let newList = starList.value.filter(
        (item) => item.dessertId !== dessertId
      )
      starList.value = newList
    }

    return {
      userInfo,
      setUserInfo,
      setUserAvatar,
      starList,
      setStarList,
      insertStarList,
      delStarList,
      updateMember,
      updatePoint,
      updateBalance,
    }
  },
  { persist: true }
)
